package com.chhuang.ewcrm.controller;

import com.chhuang.ewcrm.bean.QueryBean;
import com.chhuang.ewcrm.config.EwcrmProperties;
import com.chhuang.ewcrm.consts.EwcrmConstants;
import com.chhuang.ewcrm.entity.APrice;
import com.chhuang.ewcrm.entity.SysUser;
import com.chhuang.ewcrm.file.FileUtil;
import com.chhuang.ewcrm.service.APriceService;
import com.chhuang.ewcrm.vo.ResultBean;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * (APrice)表控制层
 *
 * @author chhuang
 * @since 2020-11-28 11:52:25
 */
@RestController
@RequestMapping("/service/aPrice")
public class APriceController {
    Logger logger = LoggerFactory.getLogger(APriceController.class);
    /**
     * 服务对象
     */
    @Resource
    private APriceService aPriceService;
    @Autowired
    private EwcrmProperties ewcrmProperties;

    /**
     * 上传价格表
     * @param pricePhoto
     * @param session
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadPrice")
    public ResultBean<?> uploadAPrice(MultipartFile pricePhoto, HttpSession session) throws IOException {
        ResultBean<?> resultBean = new ResultBean<>();
        resultBean.setSuccess(false);

        Object obj = session.getAttribute(EwcrmConstants.SessionConst.SYS_USER);

        if(obj!=null && pricePhoto!=null && !pricePhoto.isEmpty()){
            SysUser user = (SysUser) obj;

            String filename = pricePhoto.getOriginalFilename();//原文件名
            String suffixName = FileUtil.getFileSuffix(filename);//后缀名
            filename = "pricephotos/" + UUID.randomUUID().toString().replaceAll("-","") + "." + suffixName;
            String pricePhotoPath = ewcrmProperties.getUploadPath() + filename;

            File file = new File(pricePhotoPath);
            FileUtil.makeFilePath(file);
            pricePhoto.transferTo(file);

            APrice price = new APrice();
            price.setPricePic(EwcrmConstants.UPLOAD_URL + filename);
            price.setUserId(user.getUserId());
            price.setUsername(user.getUsername());
            price.setFullname(user.getFullname());
            price.setCreateTime(new Date());
            aPriceService.insert(price);

            resultBean.setSuccess(true);
        }else{
            resultBean.setMessage("参数为空或者用户没有登录");
        }

        return resultBean;
    }

    /**
     * 删除价格表
     * @param priceId
     * @return
     */
    @DeleteMapping("/removeOne/{priceId}")
    public ResultBean<?> removeOne(@PathVariable("priceId") String priceId){
        ResultBean<?> resultBean = new ResultBean<>();
        resultBean.setSuccess(true);
        if(StringUtils.isNotEmpty(priceId)){
            //删除价格表
            resultBean.setSuccess(this.aPriceService.deleteById(priceId));
        }else{
            resultBean.setSuccess(false);
            resultBean.setMessage("参数不为空");
        }
        logger.debug(resultBean.toString());
        return resultBean;
    }

    /**
     * 根据id列表批量删除
     * @param priceIds
     * @return
     */
    @PutMapping("/removeList")
    public ResultBean<?> removeList(@RequestBody List<String> priceIds){
        ResultBean<?> resultBean = new ResultBean<>();
        if(priceIds!=null && !priceIds.isEmpty()){
            resultBean.setSuccess(this.aPriceService.deleteByIds(priceIds)>0);
        }else{
            resultBean.setSuccess(false);
            resultBean.setMessage("参数不为空");
        }
        logger.debug(resultBean.toString());
        return resultBean;
    }

    /**
     * 表格查询
     * @param queryBean
     * @return
     */
    @PostMapping("/queryPage")
    public ResultBean<PageInfo<APrice>> query(@RequestBody QueryBean<APrice> queryBean){
        logger.debug(queryBean.toString());
        ResultBean<PageInfo<APrice>> resultBean = new ResultBean<>();

        PageInfo<APrice> pageInfo = null;
        List<APrice> prices = null;
        if(queryBean!=null) {
            if (StringUtils.isNotEmpty(queryBean.getOrderBy()))
                pageInfo = aPriceService.search(queryBean.getSearch(), queryBean.getPageNum(), queryBean.getPageSize(), queryBean.getOrderBy());
            else
                pageInfo = aPriceService.search(queryBean.getSearch(), queryBean.getPageNum(), queryBean.getPageSize());
            resultBean.setData(pageInfo);
            resultBean.setSuccess(true);
            resultBean.setCode(queryBean.getDraw());
        }else {
            resultBean.setSuccess(false);
            resultBean.setMessage("参数不为空");
        }

        logger.debug(resultBean.toString());
        return resultBean;
    }

}